<?php
define('IN_YuLin', true);
define('BASE_PATH', realpath(dirname(__FILE__).'/../')); //根目录
$_SERVER['HTTP_HOST'] = '';
include_once BASE_PATH . '/lib/common.inc.php';
$dbprefix = 'yulin_';
$dbname   = $_config['db']['name'];
$dbuser   = $_config['db']['user'];
$dbpass   = $_config['db']['pass'];
$dbhost   = $_config['db']['host'];
$dbport   = $_config['db']['port'];
// 脚本代码公共部分
$db = new PDO ('mysql:host=' . $dbhost . ';port=' . $dbport . ';dbname=' . $dbname, $dbuser, $dbpass,[PDO::ATTR_PERSISTENT=>false]);
$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$db->setAttribute(PDO::ATTR_PERSISTENT, true);
$db->exec('SET NAMES UTF8');

/**
 * 金额计算记录
 * @param unknown $data
 */
function moneyLog($data){
    global $db;
    global $dbprefix;
    if($data['balance'] == 0 && $data['uid'] < 0) return false;
    // 是否已经操作过
    $tmp      = getList('SELECT 1 FROM ' . $dbprefix . 'user_money_log' . ' WHERE lkey = \''.$data['lkey'].'\'', $db, '1');
    if($tmp) return false;
    // 用户现有资金
    $money    = getList('SELECT * FROM ' . $dbprefix . 'user' . ' WHERE id = ' . $data['uid'], $db, '1');
    $balance  = bcadd($money['money'], $data['balance'], 2);	//	操作后余额
    if($balance < 0) return false;
    // 冻结金额
    $freeze   = 0;
    //	资金日志
    $dateLine           = time();
    $ins = [];
    $ins['uid']			=	$data['uid'];
    $ins['tid']			=	$data['tid'];
    $ins['lkey']		=	$data['lkey'];
    $ins['money']		=	$data['money'];
    $ins['balance']		=	$balance;
    $ins['freeze']		=	$freeze;
    $ins['total']		=	$balance + $freeze;
    $ins['balance_old']	=	$money['money'];
    $ins['freeze_old']	=	$freeze;
    $ins['total_old']	=	$money['money'];
    $ins['dateline']	=	$dateLine;
    $ins['ip']			=	GetIp();
    $ins['notes']		=	$data['notes'];
    $insSql = 'INSERT INTO ' . $dbprefix . 'user_money_log' . ' ' . CreateInsertSql($ins);
    $id     = $db->exec($insSql);
    if(!$id) return false;
    //	个人账户金额
    $db->exec('UPDATE ' . $dbprefix . 'user' . ' SET money = ' . $balance . ' WHERE id = ' . $data['uid']);
    return true;
}

/**
 * 公共查询方法
 * @param unknown $sql
 * @param unknown $db
 */
function getList($sql, $db, $row='all'){
    $res  = $db->query($sql);
    $list = $res->fetchall(PDO::FETCH_ASSOC);
    if ($row == 'all') {
        return $list;
    }else{
        return array_shift($list);
    }
}
// 生成insert语句
function CreateInsertSql($arr){
    return '(`'.implode('`,`',array_keys($arr)).'`) VALUES(\''.implode('\',\'',$arr).'\')';
}

// 生成update语句
function CreateUpdateSql($arr, $sep = ',') {
    $sql = $com = '';
    foreach($arr as $k => $v) {
        $sql .= $com.'`'.$k.'` = \''.$v.'\'';
        $com = $sep;
    }
    return $sql;
}

// 获取客户端ip
function GetIp()
{
    if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
    {
        $onlineip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown'))
    {
        $onlineip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
    {
        $onlineip = getenv('REMOTE_ADDR');
    }
    elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
    {
        $onlineip = $_SERVER['REMOTE_ADDR'];
    }
    if (!preg_match('/[\d\.]{7,15}/', $onlineip))
    {
        $onlineip = '0.0.0.0';
    }
    return $onlineip;
}










